<?php
/**
 * 首页服务层
 * @author maniac
 */
namespace app\common\service;

use app\common\model\Mods;
use app\common\model\Contents;
use app\common\model\Users;
use app\common\model\Members;
use app\common\model\Links;
use app\facade\Options AS OptionsFacade;

use think\Db;


class Index extends Base
{
    /**
     * 首页统计
     */
    public function statics()
    {
        $mods = Mods::field('tbname,name')->order('id ASC')->select();
        $mods = $mods ? $mods->toArray() : [];
        $data = [
            // 信息统计
            'mods'   => [],
            // 系统信息
            'site'   => [],
            // 其他统计
            'others' => [],
        ];
        if ($mods) {
            // 信息统计
            foreach ($mods as $v) {
                // 已审核数量
                $v['checked_nums']         = Contents::where([['tbname', 'eq', $v['tbname']], ['checked', 'eq', 1]])->count();
                // 未审核数量
                $v['no_checked_nums']      = Contents::where([['tbname', 'eq', $v['tbname']], ['checked', 'eq', 0]])->count();
                // 未审核投稿
                $v['no_checked_post_nums'] = Contents::where([['tbname', 'eq', $v['tbname']], ['ismember', 'eq', 1], ['checked', 'eq', 0]])->count();
                // 总数
                $v['total']                = Contents::where([['tbname', 'eq', $v['tbname']]])->count();
                // 今日发布
                $v['today_post_nums']      = Contents::where([['tbname', 'eq', $v['tbname']]])->whereTime('post_time', 'today')->count();
                // 昨日发布
                $v['yesterday_post_nums']  = Contents::where([['tbname', 'eq', $v['tbname']]])->whereTime('post_time', 'yesterday')->count();
                // 本周发布
                $v['week_post_nums']       = Contents::where([['tbname', 'eq', $v['tbname']]])->whereTime('post_time', 'week')->count();
                // 本月发布
                $v['month_post_nums']      = Contents::where([['tbname', 'eq', $v['tbname']]])->whereTime('post_time', 'month')->count();
                $data['mods'][]            = $v;
            }
        }
        // 服务器软件
        $data['site']['software']                 = input('server.SERVER_SOFTWARE');
        // 操作系统
        $data['site']['os']                       = PHP_OS;
        // PHP 版本
        $data['site']['php_version']              = PHP_VERSION;
        // MYSQL 版本
        $data['site']['mysql_version']            = \think\Db::query("select VERSION()")[0]['VERSION()'];
        // 使用域名
        $data['site']['domain']                   = input('server.SERVER_NAME');
        // 程序编码
        $data['site']['charset']                  = mb_detect_encoding('程序编码');
        
        // 其他统计
        // 待审核会员数量
        $data['others']['no_checked_member_nums'] = Members::where('checked', 0)->count();
        // 会员总数
        $data['others']['checked_member_nums']    = Members::where('checked', 1)->count();
        // 管理员总数
        $data['others']['admin_nums']             = Users::count();
        // 待审核留言数量
        $data['others']['no_checked_geustbook']   = 0;
        // 留言总数
        $data['others']['geustbook_nums']         = 0;
        // 未查看反馈数量
        $data['others']['no_checked_feedback']    = 0;
        // 反馈总数
        $data['others']['feedback_nums']          = 0;
        
        return $data;
    }


    //接口访问记录
    public function statistical($controller,$action,$ip,$parameter){
        $tableName = 'statistical'.date('Y-m-d');
        $this->createTable($tableName);
        Db::name($tableName)->insert(['controller'=>$controller.'/'.$action,'ip'=>$ip,'create_time'=>time(),'parameter'=>json_encode($parameter)]);
        return;
    }
    //创建数据库
    public function createTable($tableName)
    {
        $tableName = 'maniac_'.$tableName;
        $sql = "CREATE TABLE IF NOT EXISTS `$tableName` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `controller` varchar(255) NOT NULL COMMENT '',
        `ip` varchar(64) DEFAULT NULL COMMENT '',
        `create_time` int(11) NOT NULL COMMENT '',
        `parameter` varchar (1000) NOT NULL COMMENT '',
        PRIMARY KEY (`id`)
        )
        ENGINE=InnoDB
        DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
        CHECKSUM=0
        ROW_FORMAT=DYNAMIC
        DELAY_KEY_WRITE=0";

        Db::execute($sql);
    }




}
